package com.wangjiacai.hql.condition.where;

import com.wangjiacai.hql.condition.Condition;
import com.wangjiacai.util.HqlUtil;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Condition(condition = LeftLike.LikeCondition.class,column = LeftLike.LikeCondition.class,userValue = true,format = "%%%s")
public @interface LeftLike {

    String column() default "";

    String hql() default "";

    class LikeCondition implements Condition.Where<LeftLike,String>, Condition.Column<LeftLike> {
        @Override
        public String where(LeftLike leftLike,String property,String column,Object obj,String value) {
            return leftLike.hql().equals("")?column+" like '"+value+"'":column+" like ("+ HqlUtil.parseHql(leftLike.hql(),obj)+")";
        }

        @Override
        public String column(LeftLike like, String property) {
            return like.column().equals("")?property:like.column();
        }
    }

}
